package com.hsyt.pfc.support.util;

import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/**
 * @Author: wangze@hsyuntai.com
 * Date: 2016/9/28
 * Time: 下午4:12
 */
public class SealUtil {
    private SealUtil() {

    }

    public static String genPersonalSeal(String name) {
        return genPersonalSeal(name, new SealConfig());
    }

    public static String genPersonalSeal(String name, SealConfig config) {
        if (name == null || name.length() == 0)
            return null;
        String result = null;
        try {
            //加载字体
            Font fontOfText = new Font(config.fontName, Font.PLAIN, config.fontSize);
            //计算基本尺寸
            SizeFromFont size = evaluateTextSize(fontOfText, name);
            //图像大小为文字大小加两边空隙
            int graphicsWidth = size.getTextWidth() + config.horizontalMargin * 2;
            int graphicsHeight = size.getTextHeight() + config.verticalMargin * 2;

            BufferedImage bi = new BufferedImage(graphicsWidth, graphicsHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D ig2 = bi.createGraphics();
            ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            ig2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            ig2.setBackground(config.backgroundColor);
            //绘制外边框
            ig2.setColor(config.frameColor);
            ig2.setStroke(new BasicStroke(config.stroke));
            ig2.drawRect(0, 0, graphicsWidth - 1, graphicsHeight - 1);
            //绘制内边框
            ig2.setStroke(new BasicStroke(config.stroke - 3));
            ig2.drawRect(6, 6, graphicsWidth - 13, graphicsHeight - 13);
            //绘制姓名
            ig2.setFont(fontOfText);
            //计算文本坐标
            float textX = config.horizontalMargin;
            //文本的坐标以 baseline 为基准,所以要往下移一个 ascent
            float textY = config.verticalMargin + size.getAscent();
            ig2.setColor(config.textColor);
            ig2.drawString(name, textX, textY);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, config.fileFormat, baos);
            baos.flush();
            byte[] imageInByte = baos.toByteArray();
            result = DatatypeConverter.printBase64Binary(imageInByte);
        } catch (IOException ie) {
            ie.printStackTrace();
        }
        return result;
    }

    private static SizeFromFont evaluateTextSize(Font fontOfText, String text) {
        BufferedImage bi4Evaluate = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D ig4Evaluate = bi4Evaluate.createGraphics();
        ig4Evaluate.setFont(fontOfText);
        FontMetrics fontMetrics = ig4Evaluate.getFontMetrics();
        int textWidth = fontMetrics.stringWidth(text);
        int textHeight = fontMetrics.getHeight();
        int ascent = fontMetrics.getAscent();
        return new SizeFromFont(textWidth, textHeight, ascent);
    }

    private static class SizeFromFont {
        private int textWidth;
        private int textHeight;
        private int ascent;

        public SizeFromFont(int textWidth, int textHeight, int ascent) {
            this.textWidth = textWidth;
            this.textHeight = textHeight;
            this.ascent = ascent;
        }

        public int getTextWidth() {
            return textWidth;
        }

        public void setTextWidth(int textWidth) {
            this.textWidth = textWidth;
        }

        public int getTextHeight() {
            return textHeight;
        }

        public void setTextHeight(int textHeight) {
            this.textHeight = textHeight;
        }

        public int getAscent() {
            return ascent;
        }

        public void setAscent(int ascent) {
            this.ascent = ascent;
        }
    }

    public static final class SealConfig {
        private String fontName = "PingFang SC";
        private int fontSize = 32;
        private Color backgroundColor = Color.WHITE;
        private Color frameColor = Color.decode("#E5342A");
        ;
        private Color textColor = Color.decode("#E5342A");
        private String fileFormat = "PNG";
        private int horizontalMargin = 30;
        private int verticalMargin = 20;
        private int stroke = 4;

        public SealConfig fontName(String fontName) {
            this.fontName = fontName;
            return this;
        }

        public SealConfig fontSize(int size) {
            this.fontSize = size;
            return this;
        }

        public SealConfig frameColor(Color color) {
            this.frameColor = color;
            return this;
        }

        public SealConfig textColor(Color color) {
            this.textColor = color;
            return this;
        }

        public SealConfig fileFormat(String format) {
            this.fileFormat = format;
            return this;
        }

        public SealConfig horizontalMargin(int margin) {
            this.horizontalMargin = margin;
            return this;
        }

        public SealConfig verticalMargin(int vmargin) {
            this.verticalMargin = vmargin;
            return this;
        }

        public SealConfig stroke(int stroke) {
            this.stroke = stroke;
            return this;
        }
    }

    public static void main(String[] args) {
        SealUtil.genPersonalSeal("张长章");
    }
}